<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
          <a-col :xl='24' :lg='7' :md='8' :sm='24'>
            <a-radio-group v-model='queryParam.issueStatus' @change='radioChange'>
              <a-form-item label='回款状态'>
                <a-radio :value='1'>已审未付</a-radio>
                <a-radio :value='2'>全部</a-radio>
              </a-form-item>
            </a-radio-group>
          </a-col>
        </a-row>
        <a-row :gutter='24'>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='客户简称'>
              <a-input placeholder='请输入客户简称' v-model='queryParam.customerName' />
            </a-form-item>
          </a-col>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='项目名称'>
              <a-input placeholder='请输入投标项目名称' v-model='queryParam.projectName' />
            </a-form-item>
          </a-col>
          <template v-if='toggleSearchStatus'>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='保证金类别'>
                <j-dict-select-tag placeholder='请选择保证金类别' v-model='queryParam.depositType'
                                   dictCode='icm_deposit_type' />
              </a-form-item>
            </a-col>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='是否中标'>
                <j-dict-select-tag placeholder='请选择是否中标' v-model='queryParam.isBidding' dictCode='switch_status' />
              </a-form-item>
            </a-col>
            <a-col :xl='12' :lg='6' :md='8' :sm='12'>
              <a-form-item label='付款时间'>
                <j-date
                  placeholder='请选择开始时间'
                  class='query-group-cust'
                  v-model='queryParam.issueTime_begin'
                ></j-date>
                <span class='query-group-split-cust'></span>
                <j-date
                  placeholder='请选择结束时间'
                  class='query-group-cust'
                  v-model='queryParam.issueTime_end'
                ></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
              <a @click='handleToggleSearch' style='margin-left: 8px'>
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button @click='handleAdd' type='primary' icon='plus'>新增</a-button>
      <a-button type='primary' icon='download' @click="handleExportXls('保证金审批')">导出</a-button>
      <a-upload name='file' :showUploadList='false' :multiple='false' :headers='tokenHeader' :action='importExcelUrl'
                @change='handleImportExcel'>
        <a-button type='primary' icon='import'>导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <!--      <j-super-query :fieldList='superFieldList' ref='superQueryModal'
                           @handleSuperQuery='handleSuperQuery'></j-super-query>-->
      <a-dropdown v-if='selectedRowKeys.length > 0'>
        <a-menu slot='overlay'>
          <a-menu-item key='1' @click='batchDel'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'> 批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        :scroll='{x:true}'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        class='j-table-force-nowrap'
        @change='handleTableChange'>
        <template slot='bpmStatus_dictText' slot-scope='text, record'>
          <BpmStatusTagComponent :record='record' :text='text'></BpmStatusTagComponent>
        </template>
        <template slot='htmlSlot' slot-scope='text,record'>
          <a @click='handleDetail(record)'>{{ text }}</a>
        </template>
        <span slot='action' slot-scope='text, record'>
            <a @click='handleEdit(record)' v-if='record.bpmStatus == 1'>编辑</a>
            <a @click='handleArchived(record)' v-if="record.bpmStatus == 3&&record.issue === 'N'">付款</a>
           <a @click='handleArchived(record)'
              v-if="record.bpmStatus == 3&&record.archived === 'N'&&record.issue === 'Y'">归档</a>
            <a-divider type='vertical'
                       v-if="record.bpmStatus == 1 || record.bpmStatus == 3&&record.issue === 'N'|| record.bpmStatus == 3&&record.archived === 'N'&&record.issue === 'Y'" />
            <a-dropdown>
              <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
              <a-menu slot='overlay'>
                  <!-- <a-menu-item>
                    <a @click='handleDetail(record)'>详情</a>
                </a-menu-item> -->
                <a-menu-item v-if="record.bpmStatus === '1'">
                  <a @click='startProcess(record)' v-if="record.bpmStatus === '1'">发起流程</a>
                </a-menu-item>
                <a-menu-item v-if="record.bpmStatus === '1'">
                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                  <a style="color:red">删除</a>
                </a-popconfirm>
                </a-menu-item>
                <a-menu-item v-else-if="record.bpmStatus === '2'">
                  <a-popconfirm title='确定要作废流程吗?' @confirm='() => invalidProcess(record)'>
                    <a>作废流程</a> </a-popconfirm>
                </a-menu-item>
                <a-menu-item v-if="record.bpmStatus !== '1'"
                             @click='handleTrack(record)'>审批进度
                </a-menu-item>
              </a-menu>
            </a-dropdown>
        </span>
      </a-table>
    </div>
    <a-row style='display: flex'>
      <a-col style='flex: 1;margin-right: 5px'>
        <a-card>
          <a-statistic title='支付金额（元）' :value='countMoney' />
        </a-card>
      </a-col>
      <a-col style='flex: 1;;margin-right: 5px'>
        <a-card>
          <a-statistic title='待退（元）' :value='countNotSend' />
        </a-card>
      </a-col>
      <a-col style='flex: 1;;margin-right: 5px'>
        <a-card>
          <a-statistic title='已退（元）' :value='countAlreadySend' />
        </a-card>
      </a-col>
      <a-col style='flex: 1;;margin-right: 5px'>
        <a-card>
          <a-statistic title='中标服务费（元）' :value='countBiddingMoney' />
        </a-card>
      </a-col>
      <a-col style='flex: 1;'>
        <a-card>
          <a-statistic title='投标费用（元）' :value='countTenderMoney' />
        </a-card>
      </a-col>
    </a-row>

    <bid-deposit-apply-modal ref='modalForm' @ok='modalFormOk'></bid-deposit-apply-modal>
    <bpm-process-track-modal ref='bpmProcessTrackModal'></bpm-process-track-modal>
    <bpm-biz-task-deal-modal ref='taskDealModal' :path='path'
                             :formData='formData' @ok='handleOk'></bpm-biz-task-deal-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgBpmListMixin } from '@/mixins/JeecgBpmListMixin'
import BidDepositApplyModal from './modules/BidDepositApplyModal'
import BpmProcessTrackModal from '@views/modules/bpmbiz/common/BpmProcessTrackModal'
import BpmBizTaskDealModal from '@views/modules/bpmbiz/common/BpmBizTaskDealModal'
import { getAction, postAction } from '@/api/manage'
import { filterDictText, filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'
import BpmStatusTagComponent from '@comp/bpm/bpmStatusTagComponent'

export default {
  name: 'BidDepositApplyList',
  mixins: [JeecgBpmListMixin, mixinDevice],
  components: {
    BidDepositApplyModal,
    filterMultiDictText,
    BpmProcessTrackModal,
    BpmBizTaskDealModal,
    BpmStatusTagComponent
  },
  data() {
    return {
      description: '保证金审批管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'projectName',
          scopedSlots: { customRender: 'htmlSlot' }
        },
        {
          title: '客户简称',
          align: 'center',
          dataIndex: 'customerName'
        },
        {
          title: '销售经理',
          align: 'center',
          dataIndex: 'projectCharger'
        },
        {
          title: '保证金类别',
          align: 'center',
          dataIndex: 'depositType_dictText'
        },
        {
          title: '支付金额',
          align: 'center',
          dataIndex: 'deposit'
        },
        {
          title: '付款时间',
          align: 'center',
          dataIndex: 'issueTime',
          sorter: true
        },
        {
          title: '退回金额',
          align: 'center',
          dataIndex: 'returnMoney'
        },
        {
          title: '退回时间',
          align: 'center',
          dataIndex: 'returnDate',
          sorter: true
        },
        {
          title: '是否中标',
          align: 'center',
          dataIndex: 'isBidding_dictText'
        },
        {
          title: '中标服务费',
          align: 'center',
          dataIndex: 'biddingMoney'
        },
        {
          title: '投标费用',
          align: 'center',
          dataIndex: 'tenderMoney'
        },
        {
          title: '付款状态',
          align: 'center',
          dataIndex: 'depositStatus_dictText'
        },
        {
          title: '审批状态',
          align: 'center',
          dataIndex: 'bpmStatus_dictText',
          scopedSlots: { customRender: 'bpmStatus_dictText' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/bid/bidDepositApply/list',
        sum: '/bid/bidDepositApply/sum',
        delete: '/bid/bidDepositApply/delete',
        deleteBatch: '/bid/bidDepositApply/deleteBatch',
        exportXlsUrl: '/bid/bidDepositApply/exportXls',
        importExcelUrl: 'bid/bidDepositApply/importExcel',
        startProcess: '/act/process/extActProcess/startMutilProcess',
        issue: '/bid/bidDepositApply/issue'
      },
      // 代码生成后需手动设置流程编码
      flowCode: 'dev_hf_bid_deposit_apply_003',
      dictOptions: {},
      superFieldList: [],
      queryModel: true,
      countMoney: '',
      countBiddingMoney: '',
      countTenderMoney: '',
      countNotSend: '',
      countReturnMoney: '',
      countAlreadySend: '',
      queryParam: {
        issueStatus: 1 //默认未付
      }
    }
  },
  created() {
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    getSum(params) {
      getAction(this.url.sum, params).then((res) => {
        if (res.success) {
          this.countMoney = res.result.countMoney.toFixed(2)
          this.countBiddingMoney = res.result.countBiddingMoney.toFixed(2)
          this.countTenderMoney = res.result.countTenderMoney.toFixed(2)
          this.countNotSend = (res.result.countMoney - res.result.countTenderMoney - res.result.countBiddingMoney - res.result.countReturnMoney).toFixed(2)
          this.countAlreadySend = res.result.countReturnMoney.toFixed(2)
        }
      })
    },
    startProcess(record) {
      this.$confirm({
        title: '提示',
        content: '确认提交流程吗?',
        onOk: () => {
          let params = {
            flowCode: this.flowCode,
            id: record.id,
            formUrl: 'hifar/bid/modules/BidDepositApplyForm',
            formUrlMobile: ''
          }
          postAction(this.url.startProcess, params).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
              this.onClearSelected()
              // this.handleIssue(record)
            } else {
              this.$message.warning(res.message)
            }
          }).catch((e) => {
            this.$message.warning('不识别的请求!')
          })
        }
      })
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams()//查询条件
      this.getSum(params)
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        this.loading = false
      })
    },
    searchReset() {
      this.queryParam = {}
      this.queryParam.issueStatus = 1
      this.loadData(1)
    },
    radioChange() {
      this.loadData()
    },
    initDictConfig() {
      initDictOptions('bpm_status').then((res) => {
        if (res.success) {
          this.bpmStatusDictOptions = res.result
        }
      })
    },
    handleArchived: function(record) {
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.isPay = true
      this.$refs.modalForm.edit(record)
    },
    handleIssue(params) {
      if (params.bpmStatus === '1' || params.bpmStatus === '3') {
        getAction(this.url.issue, { id: params.id }).then((res) => {
          this.loadData()
        })
      }
    }
  },
  popupCallback(row) {
    let modal = Object.assign(this.queryParam, row)
    this.queryParam = Object.assign({}, modal)
  },
  onDateChange: function(value, dateString) {
    console.log(dateString[0], dateString[1])
    this.queryParam.createTime_begin = dateString[0]
    this.queryParam.createTime_end = dateString[1]
    this.queryParam.issueTime_begin = dateString[0]
    this.queryParam.issueTime_end = dateString[1]
  }

}

</script>
<style scoped>
@import '~@assets/less/common.less';
</style>